<template>
  <van-nav-bar title="退货申请" left-text="" left-arrow @click-left="goBack" />
  <section class="aui-flexView">
    <!-- 订单详细 -->
    <section class="aui-scrollView">
      <!-- 订单状态 -->
      <div class="aui-pay-change">
        <div class="aui-flex">
          <div class="aui-flex-box">
            <h3>{{ info.status_text }}</h3>
          </div>
          <div class="aui-pay-zfb">
            <img src="/assets/images/icon-zfb.png" alt="" />
          </div>
        </div>
      </div>

      <!-- 收货地址 -->
      <div class="aui-flex">
        <div class="aui-flex-add">
          <van-image round fit :src="LoginUser.avatar" />
        </div>
        <div class="aui-flex-box">
          <h3>
            {{ info.consignee }} <em>{{ info.mobile }}</em>
          </h3>
          <p>{{ info.address_text }}</p>
        </div>
      </div>

      <!-- 退货信息 -->
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="status_text"
            is-link
            readonly
            name="picker"
            label="退款原因"
            placeholder="请选择退款原因"
            @click="showPicker = true"
            :rules="[{ required: true, message: '请选择退款原因' }]"
          />
          <van-popup v-model:show="showPicker" position="bottom">
            <van-picker
              :columns="columns"
              @confirm="onConfirm"
              @cancel="showPicker = false"
            />
          </van-popup>
          <van-field
            v-model="refundreason"
            rows="2"
            autosize
            label="退款原因"
            type="textarea"
            maxlength="50"
            placeholder="请输入退款原因"
            show-word-limit
            :rules="[{ required: true, message: '请填写退款原因' }]"
          />
        </van-cell-group>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </section>
  </section>
</template>
    <script setup>
import { showImagePreview } from "vant";
const route = useRoute();
const { cookies } = useCookies();
const goBack = () => {
  history.back();
};
const orderid = ref(route.query.orderid ?? 0);
const info = ref({});
const LoginUser = reactive(cookies.get("LoginUser"));
const showPicker = ref(false);
const columns = [
  { text: "仅退款", value: "-1" },
  { text: "退货退款", value: "-2" },
];
const status_text = ref("");
const status = ref("");
const refundreason = ref("");
const onConfirm = ({ selectedOptions }) => {
  status_text.value = selectedOptions[0]?.text;
  showPicker.value = false;
  status.value = selectedOptions[0]?.value;
};
onBeforeMount(() => {
  getInfoData();
});
const getInfoData = async () => {
  let result = await Api.business.orderInfo({
    orderid: orderid.value,
    busid: LoginUser.id,
  });
  if (result.code == 1) {
    info.value = result.data;
  }
};

const onSubmit = async () => {
  let result = await Api.business.orderRefund({
    orderid: orderid.value,
    busid: LoginUser.id,
    status: status.value,
    refundreason: refundreason.value,
  });
  if (result.code == 1) {
    showSuccessToast({
      message: result.msg,
      onClose: function () {
        goBack();
      },
    });
  } else {
    showFailToast(result.msg);
  }
};
</script>
<style>
@import url("/assets/css/info.css");
</style>